package com.jieyang.oa.service.impl;

import com.jieyang.oa.dao.AuditingDao;
import com.jieyang.oa.dao.ExpenseDao;
import com.jieyang.oa.dao.PaymentDao;
import com.jieyang.oa.pojo.entity.Auditing;
import com.jieyang.oa.pojo.entity.Expense;
import com.jieyang.oa.pojo.entity.Payment;
import com.jieyang.oa.service.AuditingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 29276
 * @version 1.0
 * @description: TODO
 * @date 2021/8/21 16:49
 */
@Service
@Transactional
public class AuditingServiceImpl implements AuditingService {

    @Resource
    private AuditingDao auditingDao;

    @Resource
    private ExpenseDao expenseDao;

    @Resource
    private PaymentDao paymentDao;

    @Override
    public Integer insert(Auditing auditing) {

        Expense expense = expenseDao.selectByExpId(auditing.getExpId());
        auditing.setEmpId(expense.getEmpId());
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        auditing.setTime(time);


        if (auditing.getResult().equals("通过")) {
            if (expense.getNextAuditor().equals("lisi")) {
                auditingDao.insert(auditing);
                expenseDao.updateStatus(auditing.getExpId(), "已付款");
                //  TO DO
                Payment payment = new Payment();
                payment.setPayEmpId(expense.getEmpId());
                payment.setAmount(expense.getTotalAmount());
                payment.setExpId(expense.getExpId());
                payment.setPayTime(time);
                payment.setEmpId("lisi");
                paymentDao.insert(payment);
                return 4;
            }
            if (expense.getTotalAmount() <= 1000) {
                auditingDao.insert(auditing);
                expenseDao.updateStatus(auditing.getExpId(), "待付款");
                expenseDao.updateAudit(auditing.getExpId(), "lisi");
                return 1;
            } else {
                auditingDao.insert(auditing);
                if (expense.getNextAuditor().equals("jieyang")) {
                    expenseDao.updateStatus(auditing.getExpId(), "待付款");
                    expenseDao.updateAudit(auditing.getExpId(), "lisi");
                    return 1;
                } else {
                    expenseDao.updateAudit(auditing.getExpId(), "jieyang");
                    return 2;
                }
            }
        } else if (auditing.getResult().equals("拒绝")) {
            auditingDao.insert(auditing);
            expenseDao.updateStatus(auditing.getExpId(), "拒绝");
            return 0;
        } else if (auditing.getResult().equals("驳回")) {
            auditingDao.insert(auditing);
            expenseDao.updateStatus(auditing.getExpId(), "驳回");
            return 3;
        }
        return 0;
    }

    @Override
    public List<Auditing> selectByExpId(String expId) {
        return auditingDao.selectByExpId(expId);
    }

    @Override
    public List<Auditing> selectByEmpId(String empId) {
        return auditingDao.selectByEmpId(empId);
    }
}
